<template>
  <div>
    <el-dialog v-model="currShow" title="新增学生信息" width="500px" :before-close="close" :close-on-click-modal="false"
      @open="open()">
      <el-form :model="queryForm" v-loading="formLoading" :rules="rules" ref="queryRef" label-width="100px"
        class="demo-form-inline">
        <el-row>
          <el-col :span="23">
            <el-form-item label="姓名" prop="name">
              <el-input v-model.trim="queryForm.name" maxlength="20" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="queryForm.gender" class="ml-4">
                <el-radio label="男">男</el-radio>
                <el-radio label="女">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="年龄" prop="age">
              <el-input v-model.trim="queryForm.age" @input="validateNumber(queryForm, 'age')" maxlength="2"
                placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="电话" prop="tel">
              <el-input v-model.trim="queryForm.tel" @input="validateNumber(queryForm, 'tel')" maxlength="11"
                placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="父母电话" prop="parentTel">
              <el-input v-model.trim="queryForm.parentTel" @input="validateNumber(queryForm, 'parentTel')" maxlength="11"
                placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close(false)">取消</el-button>
          <el-button type="primary" @click="submit(queryRef)" :loading="submitLoading">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { addStudentInfo, editStu } from '@/api/goodsOrder/addStudent'
import { nextTick } from 'vue'
import useCurrentInstance from '@/utils/useCurrentInstance'
import { defineComponent, reactive, ref, watch } from '@vue/runtime-core'
import { ElMessage } from 'element-plus'
export default defineComponent({
  props: {
    valueShow: {
      type: Boolean,
      default: false,
    },
    stuInfo: {
      type: Object,
      default: () => { }
    }
  },
  setup(props, { emit }) {
    // 变量区域
    const { proxy } = useCurrentInstance()
    const currShow = ref(false)
    const formLoading = ref(false)
    const submitLoading = ref(false)
    const queryRef: any = ref(null)
    const queryForm: any = ref({
      stu_code: null,
      name: null,
      gender: null,
      age: null,
      tel: null,
      parentTel: null,
    })
    const rule = { required: true, message: '必填项', trigger: 'blur' }
    const rules = {
      name: [rule],
      gender: [rule],
      age: [rule],
      tel: [rule],
      gentelder: [rule],
      parentTel: [rule],
    }

    // 方法区域
    const open = () => {
      if (!props.stuInfo) return
      const { stu_name, stu_gender, stu_age, tel, parentTel, stu_code } = props.stuInfo
      nextTick(() => {
        queryForm.value.name = stu_name
        queryForm.value.gender = stu_gender
        queryForm.value.age = stu_age
        queryForm.value.tel = tel
        queryForm.value.parentTel = parentTel
        queryForm.value.stu_code = stu_code
      })
    }

    const close = (val) => {
      emit('closeStudent', val)
      nextTick(() => {
        queryRef.value.resetFields()
      })
    }

    const submit = (formRef) => {
      formRef.validate((valid) => {
        if (!valid) return
        formLoading.value = true
        submitLoading.value = true
        const submitApi = props.stuInfo ? editStu : addStudentInfo
        submitApi(queryForm.value)
          .then((res: any) => {
            close(true)
            ElMessage.success(res.message)
          })
          .finally(() => {
            formLoading.value = false
            submitLoading.value = false
          })
      })
    }

    const validateNumber = (obj, val) => {
      // 用正则表达式检查输入是否合法
      // const regex = /^\d*\.?\d{0,2}$/
      // 如果输入不合法，移除最后一位字符
      // !regex.test(obj[val]) && (obj[val] = obj[val].slice(0, -1))
      obj[val] = obj[val].replace(/[^0-9]/g, '')
    }

    // 钩子函数区域
    watch(
      () => props.valueShow,
      (newVal) => {
        currShow.value = newVal
      }
    )

    // 方法回调区域

    // return返回值
    return {
      currShow,
      formLoading,
      submitLoading,
      queryRef,
      queryForm,
      rules,
      open,
      close,
      submit,
      validateNumber,
    }
  },
})
</script>
<style scoped lang="scss"></style>
